<?php
namespace App\Controllers;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

use App\Models\PowerModel;
use stdClass;
class PowerController extends BaseController {

	public function getPower($request, $response, $args) {
		include APP_PATH . "Views/Power/powerlist.html";
	}

	public function PowerSelect($request, $response, $args) {
		$roleid = $request -> getParam('roleid');
		$user = new PowerModel();
		$result = $user -> getCountRole($roleid);

		$draw = $request -> getParam('draw') * 1;
		//第几次请求
		$start = $request -> getParam('start');
		//第一条数据开始的索引
		$length = $request -> getParam('length');
		//返回的数据长度
		$newarr = new stdClass();
		$newarr -> draw = $draw;
		$newarr -> page = count($result) / $length;
		$newarr -> recordsTotal = count($result);
		$newarr -> recordsFiltered = count($result);
		$newarr -> data = array();
		$h = 0;
		for ($i = $start; $i < $start + $length; $i++, $h++) {
			if (!empty($result[$i])) {
				$newarr -> data[$h] = $result[$i];
			} else {
				break;
			}

		}
		return $newResponse = $response -> withJson($newarr, 200);
	}

	public function Menu($request, $response, $args) {
		$username = $request -> getParam('username');
		$user = new PowerModel();
		$result = $user -> getMenu($username);
		echo $result;
	}

	public function getUpdata($request, $response, $args) {
		include APP_PATH . "Views/Power/powerupdata.html";
	}

	public function PowerUpdata($request, $response, $args) {
		$roleid = $request -> getParam('rolename');
		$powers = $request -> getParam('power');
		$role = new PowerModel();
		$data = $role -> powerupdata($roleid, $powers);
//		$msg = json_decode($data);
		echo json_encode($data);
		//echo $data;
		//		echo json_encode([
		//			"roleid"=>$roleid,
		//			"powers"=>$powers
		//		]);
	}

}
?>